<?php
namespace daswork;

class View
{
    public $assign;#用于判断类是否存在，节约性能
 
    /**
     * 变量名替换
     * @param $name
     * @param $value
     */
    public function assign($name, $value)
    {
        $this->assign[$name] = $value;
    }
 
    /**
     * 显示视图
     * @param $file
     */
    // public function display($file)
    // {
    //     $file = APP.'/views/'.$file;
    //     if (is_file($file)){
    //         extract($this->assign);#从数组中将变量导入到当前的符号表
    //         include $file;
    //     }
    // }

    public function fetch($file = "")
    {
        if (!empty($this->assign)) {
            extract($this->assign);
        }
        if ($file) {
            $namearary = explode(".", $file);
            $typename = end($namearary);
            if (!in_array($typename, ['html','tpl','htm','php'])) {
                $file=$file . '.' . TPL;
            }
            
            $filename = APP_PATH."/".$GLOBALS['model']."/view/" . strtolower($GLOBALS['ctrl']) . "/".$file;
            // echo '<br/>'.$filename;
            return require($filename);
        } else {
            // var_dump(APP_PATH . DS . $GLOBALS['model'] . DS . 'view' . DS .  strtolower($GLOBALS['ctrl']) . $GLOBALS['action']. TPL);
            return require(APP_PATH . DS . $GLOBALS['model'] . DS . 'view' . DS . strtolower($GLOBALS['ctrl']) . DS . $GLOBALS['action']. TPL);
        }
    }
}
